package com.fire.eyes.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.fire.core.util.AjaxResult;
import com.fire.eyes.constants.ErrorCodeConstants;
import com.fire.eyes.model.*;
import com.fire.eyes.service.*;
import com.fire.eyes.util.ImageUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Aspire
 */
@Service
public class GoodsInfoServiceImpl implements IGoodsInfoService{
	private static Logger logger = Logger.getLogger(GoodsInfoServiceImpl.class);
	
	@Autowired
	private ITMitBannerService iTMitBannerService;
	@Autowired
	private ITMitShellChannelRelationService relationService;
	@Autowired
	private ITMitShelfService iTMitShelfService;
	@Autowired
	private ImageUtil imageUtil;
	
	@Override
	public AjaxResult queryGoodShelfInfo(String json) {
		ShelfReq shelfReq = JSON.parseObject(json, ShelfReq.class);
		if(shelfReq == null){
    		return new AjaxResult(ErrorCodeConstants.GOODS_PARAMERROR,"入参不可为空","入参不可为空");
		}
		if (StringUtils.isEmpty(shelfReq.getChannelid())) {
			return new AjaxResult(ErrorCodeConstants.GOODS_PARAMERROR,"渠道号不可为空","渠道号不可为空");
		}
		GoodsShelfRes res = new GoodsShelfRes();
		EntityWrapper<TMitShellChannelRelation> ew = 
				new EntityWrapper<TMitShellChannelRelation>(new TMitShellChannelRelation());
		ew.eq("channelid", shelfReq.getChannelid());
		List<TMitShellChannelRelation> resultList = relationService.selectList(ew);
		if(resultList == null || resultList.size() == 0) {
			return new AjaxResult(ErrorCodeConstants.SUCCESS,"无商品货架或者主题数据","无商品货架或者主题数据");
		}
		//EntityWrapper<TMitShelf> ew1 = new EntityWrapper<TMitShelf>(new TMitShelf());
		Integer[] shelfIds = new Integer[resultList.size()];
		for(int i=0;i<resultList.size();i++) {
			shelfIds[i] = resultList.get(i).getShelfid();
		}
		EntityWrapper<TMitShelf> ew2 = new EntityWrapper<TMitShelf>(new TMitShelf());
		ew2.in("shelfid", shelfIds);
		List<TMitShelf> tMitShelfList = iTMitShelfService.selectList(ew2);
		if(tMitShelfList == null || tMitShelfList.size() == 0) {
			return new AjaxResult(ErrorCodeConstants.SUCCESS,"无商品货架或者主题数据","无商品货架或者主题数据");
		}
		List<GoodShelfDTO> list = new ArrayList<GoodShelfDTO>();
		for(TMitShelf tMitShelf : tMitShelfList) {
			GoodShelfDTO dto = new GoodShelfDTO();
			dto.setShelfid(tMitShelf.getShelfid()+"");
			dto.setShelfname(tMitShelf.getShelfname());
			dto.setShelfurl(imageUtil.imageUrlConvert(tMitShelf.getShelfurl()));
			list.add(dto);
		}
		res.setShelflist(list);
    	return new AjaxResult(ErrorCodeConstants.SUCCESS,"成功",res,"成功");
	}

	@Override
	public AjaxResult getBannerList(String json) {
		BannerReq bannerReq = JSON.parseObject(json, BannerReq.class);
		if(bannerReq == null){
    		return new AjaxResult(ErrorCodeConstants.GOODS_PARAMERROR,"入参不可为空","入参不可为空");
		}
		if (StringUtils.isEmpty(bannerReq.getChanelid())) {
			return new AjaxResult(ErrorCodeConstants.GOODS_PARAMERROR,"渠道号不可为空","渠道号不可为空");
		}
		if(bannerReq.getSourcetype() != null && bannerReq.getSourcetype() != 1) {
			if(bannerReq.getSourceid() == null) {
				return new AjaxResult(ErrorCodeConstants.GOODS_PARAMERROR,"sourceid不可为空","sourceid不可为空");
			}
		}
        if(bannerReq.getPlatetype()  != null) {
	        if(bannerReq.getPlatetype() != 0 && bannerReq.getPlatetype() != 1) {
	        	return new AjaxResult(ErrorCodeConstants.GOODS_PARAMERROR,"平台类型错误","平台类型错误");
	        }
        }
        if(bannerReq.getSourcetype() == null || bannerReq.getSourcetype() == 1) {
        	bannerReq.setSourcetype(1);
        	bannerReq.setSourceid(Integer.valueOf(bannerReq.getChanelid()));
        }
        logger.info("获取banner接口查询参数：bannerReq="+bannerReq);
        List<BannerDTO> bannerList = iTMitBannerService.findBannerList(bannerReq);
        if(bannerList == null || bannerList.size() == 0) {
        	return new AjaxResult(ErrorCodeConstants.SUCCESS,"无Banner数据","无Banner数据");
        }
        for(BannerDTO dto : bannerList) {
        	dto.setImageurl(imageUtil.imageUrlConvert(dto.getImageurl()));
        	dto.setBannerurl(imageUtil.imageUrlConvert(dto.getBannerurl()));
        }
		BannerRes res = new BannerRes();
		res.setBannerlist(bannerList);
		return new AjaxResult(ErrorCodeConstants.SUCCESS,"成功",res,"成功");
	}
}
